#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ----------------------------------------------------------
# -- CustomJSONEncoder
# -- 
# ****************************
# Author: lmay.Zhou
# Blog: www.lmaye.com
# @email lmay@lmaye.com
# Date: 2018年4月4日 00:16:43
# ----------------------------------------------------------
from flask.json import JSONEncoder
from flask_login import unicode


class CustomJSONEncoder(JSONEncoder):
    """
        This class adds support for lazy translation texts to Flask's
        JSON encoder. This is necessary when flashing translated texts.
    """
    def default(self, obj):
        from speaklater import is_lazy_string
        if is_lazy_string(obj):
            try:
                return unicode(obj)  # python 2
            except NameError:
                return str(obj)  # python 3
        return super(CustomJSONEncoder, self).default(obj)
